package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.AdminstratorDAO;
import cn.herlioner.pojo.Administrator;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.service.PageService;

public class AdministratorDAOImpl implements AdminstratorDAO{

	@Override
	public void add(Administrator a) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.save(a);
		
		s.getTransaction().commit();
		s.close();
		sf.close();
		
	}

	@Override
	public Administrator get(Long long1) {
		Administrator result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (Administrator) s.get(Administrator.class, long1);
        
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(Long long1) {
		SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        Administrator p = (Administrator) s.get(Administrator.class, long1);
 
        s.delete(p);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
	}

	@Override
	public void update(Administrator a) {
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	        Session s = sf.openSession();
	        s.beginTransaction();
	 
	        s.update(a);
	 
	        s.getTransaction().commit();
	        s.close();
	        sf.close();
		
	}

	@Override
	public List<Administrator> list() {
		List<Administrator> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from Administrator a");
 
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public PageService PageSelect(int pagenum, int pagesize) {
		List<Administrator> Administratorlist = new ArrayList<>();
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        // 查询列表  
        SQLQuery q = (SQLQuery) s.createSQLQuery("select * FROM Administrator a ORDER BY a.Administrator ASC")
        		.addEntity(Administrator.class)
        		.setFirstResult((pagenum - 1) * pagesize)
        		.setMaxResults(pagesize);
        Administratorlist = q.list();
        
        //查询总数
        Long count = (Long) s.createQuery(//  
                "SELECT COUNT(*) FROM Administrator a")//  
                .uniqueResult(); 
        
        s.close();
        sf.close();
        System.out.println(Administratorlist);
        return new PageService(pagenum, pagesize, count.intValue(), Administratorlist);
	}

}
